package cn.iocoder.yudao.module.nmkj.controller.admin.tagarticle;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.nmkj.controller.admin.tagarticle.vo.*;
import cn.iocoder.yudao.module.nmkj.dal.dataobject.tagarticle.TagArticleDO;
import cn.iocoder.yudao.module.nmkj.service.tagarticle.TagArticleService;

@Tag(name = "管理后台 - 标签文章")
@RestController
@RequestMapping("/nmkj/tag-article")
@Validated
public class TagArticleController {

    @Resource
    private TagArticleService tagArticleService;

    @PostMapping("/create")
    @Operation(summary = "创建标签文章")
    @PreAuthorize("@ss.hasPermission('nmkj:tag-article:create')")
    public CommonResult<Long> createTagArticle(@Valid @RequestBody TagArticleSaveReqVO createReqVO) {
        return success(tagArticleService.createTagArticle(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新标签文章")
    @PreAuthorize("@ss.hasPermission('nmkj:tag-article:update')")
    public CommonResult<Boolean> updateTagArticle(@Valid @RequestBody TagArticleSaveReqVO updateReqVO) {
        tagArticleService.updateTagArticle(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除标签文章")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('nmkj:tag-article:delete')")
    public CommonResult<Boolean> deleteTagArticle(@RequestParam("id") Long id) {
        tagArticleService.deleteTagArticle(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得标签文章")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('nmkj:tag-article:query')")
    public CommonResult<TagArticleRespVO> getTagArticle(@RequestParam("id") Long id) {
        TagArticleDO tagArticle = tagArticleService.getTagArticle(id);
        return success(BeanUtils.toBean(tagArticle, TagArticleRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得标签文章分页")
    @PreAuthorize("@ss.hasPermission('nmkj:tag-article:query')")
    public CommonResult<PageResult<TagArticleRespVO>> getTagArticlePage(@Valid TagArticlePageReqVO pageReqVO) {
        PageResult<TagArticleDO> pageResult = tagArticleService.getTagArticlePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, TagArticleRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出标签文章 Excel")
    @PreAuthorize("@ss.hasPermission('nmkj:tag-article:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportTagArticleExcel(@Valid TagArticlePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TagArticleDO> list = tagArticleService.getTagArticlePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "标签文章.xls", "数据", TagArticleRespVO.class,
                        BeanUtils.toBean(list, TagArticleRespVO.class));
    }

}